
const { Op } = require("sequelize");
const { isEmpty, isNotEmpty } = require('../utils/DataTypeUtil')
/**
 */
class CriteriaExample {
  type = 'and';
  ands = [];
  ors = [];
  where = {};
  model = {}; // 查询模型
  limit = null;
  offset = null;
  constructor(model) {
    // 添加查询模型
    this.addModel(model)
    // 设置分页参数
    this.pagination(model?.page, model?.pageSize)
  }

  getCurrentType() {
    return this.type == 'OR' ? this.ors : this.ands
  }

  or() {
    this.type = 'OR'
    return this
  }

  and() {
    this.type = 'AND'
    return this
  }

  /**
   * 不等于
   * @param {*} field 
   * @param {*} value 
   * @returns 
   */
  not(field, value) {
    if (!isEmpty(value)) {
      this.getCurrentType().push({
        [field]: {
          [Op.not]: value
        }
      })
    }
    if (!isEmpty(this.model[field])) {
      value = this.model[field]
    }
    return this
  }

  /**
   * 等于
   * @param {*} field 
   * @param {*} value 
   * @returns 
   */
  eq(field, value) {
    if (!isEmpty(value)) {
      this.getCurrentType().push({ [field]: value })
    }
    if (!isEmpty(this.model[field])) {
      value = this.model[field]
      this.getCurrentType().push({ [field]: value })
    }
    return this
  }

  like(field, value) {
    if (!isEmpty(value)) {
      this.getCurrentType().push({
        [field]: {
          [Op.like]: `%${value}%`
        }
      })
    }
    if (!isEmpty(this.model[field])) {
      value = this.model[field]
      this.getCurrentType().push({
        [field]: {
          [Op.like]: `%${value}%`
        }
      })
    }
    return this
  }

  /**
   * 间隔范围
   * @param {*} field 
   * @param {*} startValue 
   * @param {*} endValue 
   * @returns 
   */
  between(field, value) {
    if (isEmpty(value) && isEmpty(this.model[field])) return this
    if (this.model[field]) {
      value = this.model[field]
    } else {
      return this
    }
    this.getCurrentType().push({
      [field]: {
        [Op.between]: value
      }
    })
    return this
  }

  pagination(page, pageSize) {
    if (page > 0 && pageSize > 0) {
      this.page = Number(page)
      this.pageSize = Number(pageSize)
      this.setPage((this.page - 1) * this.pageSize, this.pageSize)
      return this.getPage()
    }
    return {}
  }

  setPage(page, pageSize) {
    this.offset = page
    this.limit = pageSize
  }

  getPage() {
    console.log(this.offset, isNotEmpty(this.offset))
    console.log(this.limit, isNotEmpty(this.limit))
    if (isNotEmpty(this.limit) && isNotEmpty(this.offset)) {

      return {
        limit: this.limit,
        offset: this.offset
      }
    }
    return {}
  }

  getWhere() {
    if (this.ands.length > 0) {
      this.where[Op.and] = this.ands
    }
    if (this.ors.length > 0) {
      this.where[Op.or] = this.ors
    }
    return this.where
  }

  /**
   * 添加条件模型 对象
   * @param {*} model 
   */
  addModel(model) {
    this.model = model
    return this
  }
}


const ce = new CriteriaExample().addModel({
  id: 1,
  name: 'xxxxx',
  age: 18
})

ce.and().eq('id', 12)

console.log(ce.getWhere())

module.exports = {
  CriteriaExample
}